package ge.telasi.tasks.report;

import java.io.IOException;
import java.io.InputStream;
import java.text.DecimalFormat;
import java.text.NumberFormat;
import java.util.Calendar;
import java.util.HashMap;
import java.util.Map;

/**
 * Reading report files.
 * 
 * @author dimitri
 */
public class FileReader {

    /**
     * Returns default set of properties.
     */
    public static Map<String, String> getDefaultProperties() {
        Map<String, String> props = new HashMap<String, String>();
        props.put("small-font-size", "8pt");
        props.put("tiny-font-size", "6pt");
        props.put("def-font-name", "DejaVuSans");
        props.put("def-font-size", "10pt");
        props.put("title-font-name", "DejaVuSerif");
        props.put("title-font-size", "14pt");
        return props;
    }

    /**
     * Read file for the given name and puts properties.
     */
    public static String readFile(String name, Map<String, String> properties) throws IOException {
        InputStream in = FileReader.class.getResourceAsStream("xml/" + name);
        byte[] buff = new byte[1024];
        byte[] fullarray = null;
        int length;
        while ((length = in.read(buff)) != -1) {
            int startpostion = fullarray == null ? 0 : fullarray.length;
            int newSize = startpostion + length;
            byte[] newarray = new byte[newSize];
            if (fullarray != null) {
                System.arraycopy(fullarray, 0, newarray, 0, fullarray.length);
            }
            System.arraycopy(buff, 0, newarray, startpostion, length);
            fullarray = newarray;
        }
        String text = new String(fullarray, "UTF-8");
        for (String key : properties.keySet()) {
            text = text.replace("${" + key + "}", properties.get(key));
        }
        return text;
    }

    /**
     * Format given string as squared.
     */
    public static String squaredString(String text) {
        StringBuilder b = new StringBuilder();
        for (int i = 0; i < text.length(); i++) {
            char ch = text.charAt(i);
            b.append("<fo:inline ");
            b.append("font-size=\"10pt\" ");
            b.append("font-family=\"DejaVuSans\" ");
            b.append("padding-top=\"3pt\" ");
            b.append("padding-bottom=\"1pt\" ");
            if (ch == '/') {
                b.append("padding-left=\"2pt\" ");
                b.append("padding-right=\"2pt\" ");
            } else {
                b.append("padding-left=\"5pt\" ");
                b.append("padding-right=\"5pt\" ");
                b.append("border-left=\"1pt solid black\" ");
                b.append("border-top=\"1pt solid black\" ");
                b.append("border-bottom=\"1pt solid black\" ");
                if (i == text.length() - 1 || text.charAt(i + 1) == '/') {
                    b.append("border-right=\"1pt solid black\" ");
                }
            }
            if (ch == ' ') {
                b.append("color=\"white\" ");
            }
            b.append(">");
            b.append(ch == ' ' ? 'x' : ch);
            b.append("</fo:inline>");
        }
        return b.toString();
    }

    /**
     * Prepare date table text.
     */
    public static String dateString(Calendar cal) throws IOException {
        String year = String.valueOf(cal.get(Calendar.YEAR));
        String month = nf.format(cal.get(Calendar.MONTH) - Calendar.JANUARY + 1);
        String date = nf.format(cal.get(Calendar.DAY_OF_MONTH));
        Map<String, String> props = getDefaultProperties();
        props.put("year", squaredString(year));
        props.put("month", squaredString(month));
        props.put("date", squaredString(date));
        String text = readFile("date.xml", props);
        return text;
    }
    private static NumberFormat nf = new DecimalFormat("00");
}
